const fp = require('lodash/fp')

const cars = [{
    name: 'Ferrari ff',
    horsepower: 660,
    dollar_value: 70000,
    in_stock: true
  },
  {
    name: 'Spyker c12 zagato',
    horsepower: 650,
    dollar_value: 648000,
    in_stock: false
  },
  {
    name: 'Jaguar xkr-s',
    horsepower: 550,
    dollar_value: 132000,
    in_stock: false
  },
  {
    name: 'audi r8',
    horsepower: 525,
    dollar_value: 114200,
    in_stock: false
  },
  {
    name: 'aston martin one-77',
    horsepower: 750,
    dollar_value: 1850000,
    in_stock: true
  },
  {
    name: 'pagani huayra',
    horsepower: 700,
    dollar_value: 1300000,
    in_stock: false
  }
]


let isLastInStock = fp.flowRight(fp.prop('in_stock'), fp.last)
let isFirstCar = fp.flowRight(fp.prop('name'), fp.first)
let _average = function (xs) {
  return fp.reduce(fp.add, 0, xs) / xs.length
}
// let averageDollarValue = function (cars) {
// //   let dollar_value = fp.map(function (car) {
// //     return car.dollar_value
// //   }, cars)
// //   return _average(dollar_value)
// // }
let averageDollarValue = fp.flowRight(_average, fp.map(car => car.dollar_value))
averageDollarValue(cars)

let _underscore = fp.replace(/\W+/g, '_')

let eachCar = car => car.name
let sanitizeNames = fp.map(fp.flowRight(_underscore, eachCar(car)))
sanitizeNames(cars)